package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocBookDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocBookCommand;
import com.cskaoyan.wordmemorize.request.VocBookPageRequest;
import com.cskaoyan.wordmemorize.service.VocBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/voc")
public class AdminVocBookController {

    @Autowired
    VocBookService vocBookService;

    @SaAdminCheckPermission("admin:book:search")
    @GetMapping("/book/list")
    public Result<PageDTO<VocBookDTO>> getBookList(VocBookPageRequest pageRequest){
        PageDTO<VocBookDTO> bookPage = vocBookService.getBookPage(pageRequest);
        return Result.ok(bookPage);
    }

    @SaAdminCheckPermission("admin:book:add")
    @PostMapping("/book")
    public Result addBook(@RequestBody VocBookCommand vocBookCommand){
        vocBookService.addVocBook(vocBookCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:book:update")
    @PutMapping("/book")
    public Result updateBook(@RequestBody VocBookCommand vocBookCommand){
        vocBookService.updateVocBook(vocBookCommand);
        return Result.ok();
    }
    @SaAdminCheckPermission("admin:book:delete")
    @DeleteMapping("/book/{bookid}")
    public Result deleteBook(@PathVariable("bookid") String bookId){
        vocBookService.deleteVocBook(Long.valueOf(bookId));
        return Result.ok();
    }
    @SaAdminCheckPermission("admin:book:batchdelete")
    @DeleteMapping("/books")
    public Result deleteBookBatch(@RequestBody BatchDeleteCommand batchDeleteCommand){
        vocBookService.deleteVocBooks(batchDeleteCommand);
        return Result.ok();
    }
}
